package com.acoffee.springcloud.provider.controller;

import com.acoffee.springcloud.common.entity.ResponseEntity;
import com.acoffee.springcloud.common.entity.User;
import com.acoffee.springcloud.provider.properties.MyDataProperties;
import com.acoffee.springcloud.provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author acoffee
 * @create 2022-01-19 17:24
 */
//@RefreshScope
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Value("${server.port}")
    String port;

//    @Value("${mydata.test}")
//    String test;

    @Autowired
    MyDataProperties myDataProperties;

    @RequestMapping("/test")
    public ResponseEntity test(){
        return new ResponseEntity<>(200,"test",myDataProperties.getTest());
    }

    @RequestMapping("/findById/{id}")
    public ResponseEntity<User> findById(@PathVariable int id) {
        User user = userService.findById(id);
        if (user != null) {
            return new ResponseEntity<>(20000, "port:"+port+",查询成功", user);
        } else {
            return new ResponseEntity<>(50000, "查询失败", null);
        }
    }

    @RequestMapping("/findAll")
    public ResponseEntity<List<User>> findAll() {
        List<User> users = userService.findAll();
        if (users != null) {
            return new ResponseEntity<>(20000, "查询成功", users);
        } else {
            return new ResponseEntity<>(50000, "查询失败", null);
        }
    }

    @PostMapping("/addUser")
    public ResponseEntity<Boolean> addUser(@RequestBody User user) {
        System.out.println(user);
        boolean flag = userService.addUser(user);
        if (flag) {
            return new ResponseEntity<>(20000, "添加成功");
        } else {
            return new ResponseEntity<>(50000, "添加失败");
        }
    }

    @PutMapping("/updateUser")
    public ResponseEntity<Boolean> updateUser(@RequestBody User user) {
        boolean flag = userService.updateUser(user);
        if (flag) {
            return new ResponseEntity<>(20000, "修改成功");
        } else {
            return new ResponseEntity<>(50000, "修改失败");
        }
    }

    @DeleteMapping("/deleteUser/{id}")
    public ResponseEntity<Boolean> deleteUser(@PathVariable int id) {
        boolean flag = userService.deleteUser(id);
        if (flag) {
            return new ResponseEntity<>(20000, "删除成功");
        } else {
            return new ResponseEntity<>(50000, "删除失败");
        }
    }

    @GetMapping("/timeout")
    public ResponseEntity<String> timeout() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(2000, "ok", "timeout");
    }

    @GetMapping("/ok")
    public ResponseEntity<String> ok() {
        return new ResponseEntity<>(2000, "ok", "timeout");
    }

//    @Value("${testconfig}")
//    String testconfig;

//    @GetMapping("/testConfig")
//    public ResponseEntity<String> testConfig(){
//        System.out.println("进入timeoutFallback");
//        HashMap<Object, Object> map = new HashMap<>();
//        map.put(11,11);
//        return new ResponseEntity<String>(20000,"testconfig",testconfig);
//    }
}
